package cn.wangyu.tcc.record.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.wangyu.tcc.record.domain.TransactionalRecord;
import cn.wangyu.tcc.record.mapper.TransactionalRecordMapper;
import cn.wangyu.tcc.record.service.TransactionalRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangyu
 * @version 1.0
 * @date 2022/5/23 22:49
 */
@Service
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class TransactionalRecordServiceImpl extends ServiceImpl<TransactionalRecordMapper,TransactionalRecord> implements TransactionalRecordService {

    private final TransactionalRecordMapper transactionalRecordMapper;

    public TransactionalRecord add(TransactionalRecord record) {
        TransactionalRecord transactionalRecord = transactionalRecordMapper.selectOne(new QueryWrapper<TransactionalRecord>().eq("xid", record.getXid()));
        if (transactionalRecord != null) {
            return transactionalRecord;
        }

        record.setId(IdUtil.getSnowflakeNextId());
        transactionalRecordMapper.insert(record);
        return record;
    }

    public TransactionalRecord getByXid(String xid) {
        return transactionalRecordMapper.selectOne(new QueryWrapper<TransactionalRecord>().eq("xid",xid));
    }
}
